﻿using System;
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using USClientKernel.Template;

namespace Game.Logic.Manager
{
    public class GameSceneManager : Singleton<GameSceneManager>
    {
        private Scene m_curScene;
        private AsyncOperation m_asyncOperation;
        //private Stack<GameScene> m_secneStack;

        // 初始化
        protected override void Init()
        {
            //m_secneStack = new Stack<GameScene>();
            m_curScene = SceneManager.GetActiveScene();
        }

        public string GetCurSceneName()
        {
            return m_curScene.name;
        }





        /// <summary>
        /// 同步加载场景
        /// </summary>
        /// <param name="scenePath">场景名称</param>
        /// <param name="mode">加载模式</param>
        public void LoadSceneSync(string scenePath, LoadSceneMode mode = LoadSceneMode.Single)
        {
            if (scenePath == m_curScene.name)
            {
                return;
            }
            SceneManager.LoadScene(scenePath, mode);
        }


        /// <summary>
        /// 异步加载场景
        /// </summary>
        /// <param name="sceneName">场景名称</param>
        /// <param name="_completed">完成的处理函数</param>
        public IEnumerator LoadSceneAsync(string sceneName, Action<AsyncOperation> _completed)
        {

            return LoadSceneAsync(sceneName, _completed, null);
        }


        /// <summary>
        /// 异步加载场景
        /// </summary>
        /// <param name="sceneName">场景名称</param>
        /// <param name="_completed">完成的处理函数</param>
        /// <param name="_progress">进度的处理函数</param>
        public IEnumerator LoadSceneAsync(string sceneName, Action<AsyncOperation> _completed, Action<float> _progress)
        {
            if (sceneName == m_curScene.name)
            {
                yield break;
            }

            AsyncOperation op = SceneManager.LoadSceneAsync(sceneName);
            op.allowSceneActivation = false;
            op.completed += _completed;

            while (!op.isDone)
            {
                if (op.progress >= 0.9f)
                    // op.allowSceneActivation = true;
                    if (null != _progress)
                        _progress(op.progress);
                yield return null;
            }

            yield return null;
        }
    }
}

